﻿Imports System.Data.OleDb

Public Class frmInPhieuNhapKho
    'ReportType 1- ngày; 2- tháng; 3-năm; 4-từ ngày đến ngày
    Public PhieuNhapKhoId As Integer
    Private Sub frmInPhieuNhapKho_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Me.WindowState = FormWindowState.Maximized
            Report_PhieuNhapKho()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#Region "In Phiếu Nhập Kho"
    Public Sub Report_PhieuNhapKho()
        Try
            'Lấy thông tin phiếu nhập kho
            Dim objReport As New rptPhieuNhapKho
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dtThongTinPhieuNhapKho As New DataTable
            Dim query As String
            query = "Select HoTenNguoiNhanHang,TheoLenh,So,Ngay,Cua,NhapKhoTai,NgayNhapKho from PhieuNhapKho Where PhieuNhapKhoId=" & PhieuNhapKhoId
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dtThongTinPhieuNhapKho)
            If dtThongTinPhieuNhapKho.Rows.Count > 0 Then
                formatDate2VN(dtThongTinPhieuNhapKho.Rows(0)("Ngay"))
                Dim aNgay() As String = dtThongTinPhieuNhapKho.Rows(0)("Ngay").ToString.Split("/")
                dtThongTinPhieuNhapKho.Rows(0)("Ngay") = " Ngày " & aNgay(0) & " tháng " & aNgay(1) & " năm " & aNgay(2)
                formatDate2VN(dtThongTinPhieuNhapKho.Rows(0)("NgayNhapKho"))
                Dim aNgayNhapKho() As String = dtThongTinPhieuNhapKho.Rows(0)("NgayNhapKho").ToString.Split("/")
                dtThongTinPhieuNhapKho.Rows(0)("NgayNhapKho") = " Ngày " & aNgayNhapKho(0) & " tháng " & aNgayNhapKho(1) & " năm " & aNgayNhapKho(2)
            End If
            objReport.Database.Tables("dtPhieuNhapKho").SetDataSource(dtThongTinPhieuNhapKho)
            'cvrThongKe.ReportSource = objReport
            Dim dtPhieuNhapKho As New DataTable
            query = "Select * from ChiTietPhieuNhapKho Where PhieuNhapKhoId=" & PhieuNhapKhoId
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dtPhieuNhapKho)
            dbConn.Close()
            'tao chi tiet phieu nhap kho
            'Lấy chi tiết phiếu nhập kho
            Dim ChiTietPhieuNhapKho As New DataTable
            ChiTietPhieuNhapKho = Create_ChiTietPhieuNhapKho()
            For i As Integer = 0 To dtPhieuNhapKho.Rows.Count - 1
                Dim newRow As DataRow
                newRow = ChiTietPhieuNhapKho.NewRow
                newRow.Item("IDSanPham") = dtPhieuNhapKho.Rows(i)("IDSanPham")
                newRow.Item("MaSanPham") = dtPhieuNhapKho.Rows(i)("MaSanPham")
                newRow.Item("TenSanPham") = dtPhieuNhapKho.Rows(i)("TenSanPham")
                newRow.Item("DVTSanPham") = dtPhieuNhapKho.Rows(i)("DVTSanPham")
                newRow.Item("DVT") = dtPhieuNhapKho.Rows(i)("DVT")
                newRow.Item("SoLuongTheoCTu") = dtPhieuNhapKho.Rows(i)("SoLuongTheoCTu")
                newRow.Item("SoLuongThuc") = dtPhieuNhapKho.Rows(i)("SoLuongThuc")
                newRow.Item("DonGia") = dtPhieuNhapKho.Rows(i)("DonGia")
                newRow.Item("ThanhTien") = dtPhieuNhapKho.Rows(i)("ThanhTien")
                ChiTietPhieuNhapKho.Rows.Add(newRow)
            Next

            BindDataNewOrder(ChiTietPhieuNhapKho)

            Dim phieuNhapKhoNew As DataTable
            phieuNhapKhoNew = Create_ChiTietPhieuNhapKho()
            Dim DonGia As Single
            Dim THanhTien As Single
            For i As Integer = 2 To Grid1.RowsCount - 1
                Dim newRow As DataRow
                newRow = phieuNhapKhoNew.NewRow
                'newRow.Item("DonGia") = Grid1(i, 0).Value
                'add STT
                newRow.Item("STT") = Grid1(i, 0).Value
                newRow.Item("DVTSanPham") = Grid1(i, 2).Value
                newRow.Item("DVT") = Grid1(i, 2).Value
                newRow.Item("SoLuongTheoCTu") = Grid1(i, 3).Value
                newRow.Item("SoLuongThuc") = Grid1(i, 4).Value
                DonGia = Grid1(i, 5).Value
                THanhTien = Grid1(i, 6).Value
                If DonGia = 0 Then
                    newRow.Item("DonGia") = ""
                Else
                    newRow.Item("DonGia") = DonGia
                End If
                If THanhTien = 0 Then
                    newRow.Item("ThanhTien") = ""
                Else
                    newRow.Item("ThanhTien") = THanhTien
                End If
                If IsDBNull(newRow.Item("DonGia")) Then
                    newRow.Item("TenSanPham") = Grid1(i, 1).Value
                Else
                    newRow.Item("TenSanPham") = Grid1(i, 1).Value
                End If
                phieuNhapKhoNew.Rows.Add(newRow)
            Next
            objReport.Database.Tables("dtChiTietPhieuNhapKho").SetDataSource(phieuNhapKhoNew)
            cvrThongKe.ReportSource = objReport
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#Region "Phiếu nhập kho mới"
    Private Sub CreateHeaderNewOrder()
        Try
            Grid1.ColumnsCount = 8 '--Add STT Column
            Grid1.Rows.Clear()
            'tạo header 
            Grid1.FixedRows = 1
            Grid1.Rows.Insert(0)
            Dim rowCOLSPAN As New SourceGrid.Cells.Views.Cell
            Dim fontBold As New Font(Grid1.Font, FontStyle.Bold)
            rowCOLSPAN.Font = fontBold
            Grid1(0, 0) = New SourceGrid.Cells.ColumnHeader("STT")
            Grid1(0, 0).Column.Width = 40
            Grid1(0, 0).View.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter
            Grid1(0, 0).View.Font = Grid1.Font

            Grid1(0, 1) = New SourceGrid.Cells.ColumnHeader("TÊN SẢN PHẨM, QUY CÁCH SẢN PHẨM")
            Grid1(0, 1).Column.Width = 275
            Grid1(0, 1).View.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter
            Grid1(0, 1).View.Font = Grid1.Font

            Grid1(0, 2) = New SourceGrid.Cells.ColumnHeader("ĐVT")
            Grid1(0, 2).Column.Width = 60
            Grid1(0, 2).View.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter
            Grid1(0, 2).View.Font = Grid1.Font


            Grid1(0, 3) = New SourceGrid.Cells.ColumnHeader("SỐ LƯỢNG")
            Grid1(0, 3).Column.Width = 80
            Grid1(0, 3).ColumnSpan = 2
            Grid1(0, 3).View.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter
            Grid1(0, 3).View.Font = Grid1.Font

            Grid1(0, 5) = New SourceGrid.Cells.ColumnHeader("Đơn giá")
            Grid1(0, 5).Column.Width = 80
            Grid1(0, 5).View.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter
            Grid1(0, 5).View.Font = Grid1.Font

            Grid1(0, 6) = New SourceGrid.Cells.ColumnHeader("THÀNH TIỀN")
            Grid1(0, 6).Column.Width = 80
            Grid1(0, 6).View.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter
            Grid1(0, 6).View.Font = Grid1.Font

            Grid1(0, 7) = New SourceGrid.Cells.ColumnHeader("IDSanPham")
            Grid1(0, 7).Column.Width = 0
            Grid1(0, 7).View.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter
            Grid1(0, 7).View.Font = Grid1.Font

            

            Grid1.Rows.Insert(1)
            Grid1(1, 3) = New SourceGrid.Cells.ColumnHeader("Theo C.từ")
            Grid1(1, 3).Column.Width = 80
            Grid1(1, 3).View.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter
            Grid1(1, 3).View.Font = Grid1.Font

            Grid1(1, 4) = New SourceGrid.Cells.ColumnHeader("Thực nhập")
            Grid1(1, 4).Column.Width = 80
            Grid1(1, 4).View.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter
            Grid1(1, 4).View.Font = Grid1.Font

            Grid1(0, 0).RowSpan = 2
            Grid1(0, 1).RowSpan = 2
            Grid1(0, 2).RowSpan = 2
            Grid1(0, 5).RowSpan = 2
            Grid1(0, 6).RowSpan = 2
            Grid1(0, 7).RowSpan = 2

        Catch ex As Exception

        End Try
    End Sub
    Private Sub BindDataNewOrder(ByVal dtPhieuNhapKho As DataTable)
        Try
            'clear row
            If dtPhieuNhapKho.Rows.Count < 0 Then
                Exit Sub
            End If
            '/Grid1.Controller.AddController(New ValueChangedEvent)
            Grid1.Rows.Clear()
            CreateHeaderNewOrder()
            Dim aTenSanPhamKhongTrung As New ArrayList
            Dim aDVT As New ArrayList
            aTenSanPhamKhongTrung.Add(dtPhieuNhapKho.Rows(0)("TenSanPham"))
            aDVT.Add(dtPhieuNhapKho.Rows(0)("DVT"))
            For i As Integer = 1 To dtPhieuNhapKho.Rows.Count - 1
                If Not aTenSanPhamKhongTrung.Contains(dtPhieuNhapKho.Rows(i)("TenSanPham")) Then
                    aTenSanPhamKhongTrung.Add(dtPhieuNhapKho.Rows(i)("TenSanPham"))
                    aDVT.Add(dtPhieuNhapKho.Rows(i)("DVT"))
                End If
            Next

            Dim nextRowIndex As Integer = 2
            'group title
            Dim rowCOLSPAN As New SourceGrid.Cells.Views.Cell
            Dim fontBold As New Font(Grid1.Font, FontStyle.Bold)
            rowCOLSPAN.Font = fontBold
            rowCOLSPAN.BackColor = Color.FromArgb(192, 255, 192)
            Dim rowHeader As New SourceGrid.Cells.Views.Cell
            rowHeader.Font = fontBold
            rowHeader.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter
            rowHeader.BackColor = Color.FromArgb(192, 255, 192)
            Dim cellID As New SourceGrid.Cells.Views.Cell
            cellID.ForeColor = Color.White
            Dim SumTongCTu As Single
            Dim SumTongThuc As Single
            For i As Integer = 0 To aTenSanPhamKhongTrung.Count - 1
                Grid1.Rows.Insert(nextRowIndex)
                Grid1(nextRowIndex, 0) = New SourceGrid.Cells.Cell(i + 1)
                Grid1(nextRowIndex, 0).View = rowHeader
                Grid1(nextRowIndex, 1) = New SourceGrid.Cells.Cell(aTenSanPhamKhongTrung(i))
                Grid1(nextRowIndex, 1).View = rowCOLSPAN
                Grid1(nextRowIndex, 2) = New SourceGrid.Cells.Cell(aDVT(i))
                Grid1(nextRowIndex, 2).View = rowHeader
                'Ô Tổng
                Dim currentHeaderIndex As Integer
                currentHeaderIndex = nextRowIndex
                Grid1(currentHeaderIndex, 3) = New SourceGrid.Cells.Cell("")
                Grid1(currentHeaderIndex, 3).View = rowHeader
                Grid1(nextRowIndex, 4) = New SourceGrid.Cells.Cell("")
                Grid1(nextRowIndex, 4).View = rowHeader
                Grid1(nextRowIndex, 5) = New SourceGrid.Cells.Cell("")
                Grid1(nextRowIndex, 5).View = rowCOLSPAN
                Grid1(nextRowIndex, 6) = New SourceGrid.Cells.Cell("")
                Grid1(nextRowIndex, 6).View = rowCOLSPAN
                Grid1(nextRowIndex, 7) = New SourceGrid.Cells.Cell("")
                Grid1(nextRowIndex, 7).View = rowCOLSPAN
                Dim rowDetail() As DataRow
                rowDetail = dtPhieuNhapKho.Select("TenSanPham='" & aTenSanPhamKhongTrung(i) & "'")
                nextRowIndex = nextRowIndex + 1
                Dim TongCongTheoCtu As Single = 0
                Dim TongCongThuc As Single = 0
                For j As Integer = 0 To rowDetail.Length - 1
                    Grid1.Rows.Insert(nextRowIndex)
                    'fix STT
                    Grid1(nextRowIndex, 0) = New SourceGrid.Cells.Cell("")
                    'Grid1(nextRowIndex, 0).View = cellID

                    Dim SoLuongTheoCTu As Single
                    SoLuongTheoCTu = Single.Parse(rowDetail(j)("SoLuongTheoCTu"))
                    If SoLuongTheoCTu > 1 Then
                        SoLuongTheoCTu = Single.Parse(SoLuongTheoCTu)
                    End If
                    Grid1(nextRowIndex, 1) = New SourceGrid.Cells.Cell(SoLuongTheoCTu.ToString & "x" & rowDetail(j)("DVTSanPham"))
                    Grid1(nextRowIndex, 1).View.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter
                    Grid1(nextRowIndex, 2) = New SourceGrid.Cells.Cell()
                    'tính toán ra số lượng
                    Dim sSoLuongTheoCTu As Single
                    sSoLuongTheoCTu = TinhSoLuongNhap(rowDetail(j)("IDSanPham"), rowDetail(j)("SoLuongTheoCTu"))
                    TongCongTheoCtu = TongCongTheoCtu + sSoLuongTheoCTu
                    Grid1(nextRowIndex, 3) = New SourceGrid.Cells.Cell("") 'Format(sSoLuongTheoCTu, "0.00"))
                    Dim sSoLuongThuc As Single
                    sSoLuongThuc = TinhSoLuongNhap(rowDetail(j)("IDSanPham"), rowDetail(j)("SoLuongThuc"))
                    TongCongThuc = TongCongThuc + sSoLuongThuc
                    Grid1(nextRowIndex, 4) = New SourceGrid.Cells.Cell("")
                    If sSoLuongThuc > 0 Then
                        Grid1(nextRowIndex, 4).Value = "" ' Format(sSoLuongThuc, "0.00")
                    Else
                        Grid1(nextRowIndex, 4).Value = ""
                    End If
                    'Create Editor Single
                    '/Dim editorSingle As New SourceGrid.Cells.Editors.TextBox(GetType(Single))
                    '/Dim converter As New DevAge.ComponentModel.Converter.NumberTypeConverter(GetType(Single))
                    '/editorSingle.TypeConverter = converter
                    Grid1(nextRowIndex, 5) = New SourceGrid.Cells.Cell(rowDetail(j)("DonGia"))
                    '/Grid1(nextRowIndex, 5).Editor = editorSingle
                    '/Grid1(nextRowIndex, 5).Editor.EnableEdit = True
                    Grid1(nextRowIndex, 6) = New SourceGrid.Cells.Cell(rowDetail(j)("ThanhTien"))
                    '/Grid1(nextRowIndex, 6).Editor = editorSingle
                    '/Grid1(nextRowIndex, 6).Editor.EnableEdit = True
                    Grid1(nextRowIndex, 7) = New SourceGrid.Cells.Cell(rowDetail(j)("IDSanPham"))
                    'STT
                    'Grid1(nextRowIndex, 8) = New SourceGrid.Cells.Cell(i)
                    nextRowIndex = nextRowIndex + 1
                Next
                'Convert.ToDouble(dtChiTietDonHangBan.Rows(i)("ThanhTien")).ToString("N", VietNam).ToString()
                Grid1(currentHeaderIndex, 3).Value = Convert.ToDouble(TongCongTheoCtu).ToString("N", VietNam).ToString() ' Format(TongCongTheoCtu, "0.00")
                SumTongCTu = SumTongCTu + TongCongTheoCtu
                If TongCongThuc > 0 Then
                    'Grid1(currentHeaderIndex, 4).Value = Format(TongCongThuc, "0.00")
                    Grid1(currentHeaderIndex, 4).Value = Convert.ToDouble(TongCongThuc).ToString("N", VietNam).ToString() ' Format(TongCongTheoCtu, "0.00")
                End If
                SumTongThuc = SumTongThuc + TongCongThuc
            Next
            'Insert Sum Row
            Grid1.Rows.Insert(nextRowIndex)
            Grid1(nextRowIndex, 0) = New SourceGrid.Cells.Cell("")
            Grid1(nextRowIndex, 0).ColumnSpan = 2
            Grid1(nextRowIndex, 0).View = rowCOLSPAN
            Grid1(nextRowIndex, 2) = New SourceGrid.Cells.Cell("Tổng")
            Grid1(nextRowIndex, 2).View = rowCOLSPAN
            'Tính Tổng Chứng Từ
            'Grid1(nextRowIndex, 3) = New SourceGrid.Cells.Cell(Format(SumTongCTu, "0.00"))
            Grid1(nextRowIndex, 3) = New SourceGrid.Cells.Cell(Convert.ToDouble(SumTongCTu).ToString("N", VietNam).ToString())
            Grid1(nextRowIndex, 3).View = rowCOLSPAN
            Grid1(nextRowIndex, 3).View.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter
            Grid1(nextRowIndex, 3).View.Font = fontBold
            'Tính Tổng Thực
            If SumTongThuc > 0 Then
                'Grid1(nextRowIndex, 4) = New SourceGrid.Cells.Cell(Format(SumTongThuc, "0.00"))
                Grid1(nextRowIndex, 4) = New SourceGrid.Cells.Cell(Convert.ToDouble(SumTongThuc).ToString("N", VietNam).ToString())
            Else
                Grid1(nextRowIndex, 4) = New SourceGrid.Cells.Cell("")
            End If
            Grid1(nextRowIndex, 4).View = rowCOLSPAN
            Grid1(nextRowIndex, 4).View.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter
            Grid1(nextRowIndex, 5) = New SourceGrid.Cells.Cell("")
            Grid1(nextRowIndex, 5).View = rowCOLSPAN
            Grid1(nextRowIndex, 6) = New SourceGrid.Cells.Cell("")
            Grid1(nextRowIndex, 6).View = rowCOLSPAN
        Catch ex As Exception

        End Try
    End Sub
    'Private Sub BindDataNewOrder(ByVal dtPhieuNhapKho As DataTable)
    '    Try
    '        'clear row
    '        If dtPhieuNhapKho.Rows.Count < 0 Then
    '            Exit Sub
    '        End If
    '        Grid1.Rows.Clear()
    '        CreateHeaderNewOrder()
    '        Dim aTenSanPhamKhongTrung As New ArrayList
    '        Dim aDVT As New ArrayList
    '        aTenSanPhamKhongTrung.Add(dtPhieuNhapKho.Rows(0)("TenSanPham"))
    '        aDVT.Add(dtPhieuNhapKho.Rows(0)("DVT"))
    '        For i As Integer = 1 To dtPhieuNhapKho.Rows.Count - 1
    '            If Not aTenSanPhamKhongTrung.Contains(dtPhieuNhapKho.Rows(i)("TenSanPham")) Then
    '                aTenSanPhamKhongTrung.Add(dtPhieuNhapKho.Rows(i)("TenSanPham"))
    '                aDVT.Add(dtPhieuNhapKho.Rows(i)("DVT"))
    '            End If
    '        Next

    '        Dim nextRowIndex As Integer = 2
    '        'group title
    '        Dim rowCOLSPAN As New SourceGrid.Cells.Views.Cell
    '        Dim fontBold As New Font(Grid1.Font, FontStyle.Bold)
    '        rowCOLSPAN.Font = fontBold
    '        rowCOLSPAN.BackColor = Color.FromArgb(192, 255, 192)
    '        Dim rowHeader As New SourceGrid.Cells.Views.Cell
    '        rowHeader.Font = fontBold
    '        rowHeader.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter
    '        rowHeader.BackColor = Color.FromArgb(192, 255, 192)
    '        Dim cellID As New SourceGrid.Cells.Views.Cell
    '        cellID.ForeColor = Color.White
    '        For i As Integer = 0 To aTenSanPhamKhongTrung.Count - 1
    '            Grid1.Rows.Insert(nextRowIndex)
    '            Grid1(nextRowIndex, 0) = New SourceGrid.Cells.Cell(i + 1)
    '            Grid1(nextRowIndex, 0).View = rowHeader
    '            Grid1(nextRowIndex, 1) = New SourceGrid.Cells.Cell(aTenSanPhamKhongTrung(i))
    '            Grid1(nextRowIndex, 1).View = rowCOLSPAN
    '            Grid1(nextRowIndex, 2) = New SourceGrid.Cells.Cell(aDVT(i))
    '            Grid1(nextRowIndex, 2).View = rowHeader
    '            'Ô Tổng
    '            Dim currentHeaderIndex As Integer
    '            currentHeaderIndex = nextRowIndex
    '            Grid1(currentHeaderIndex, 3) = New SourceGrid.Cells.Cell("")
    '            Grid1(currentHeaderIndex, 3).View = rowHeader
    '            Grid1(nextRowIndex, 4) = New SourceGrid.Cells.Cell("")
    '            Grid1(nextRowIndex, 4).View = rowHeader
    '            Grid1(nextRowIndex, 5) = New SourceGrid.Cells.Cell("")
    '            Grid1(nextRowIndex, 5).View = rowCOLSPAN
    '            Grid1(nextRowIndex, 6) = New SourceGrid.Cells.Cell("")
    '            Grid1(nextRowIndex, 6).View = rowCOLSPAN
    '            Grid1(nextRowIndex, 7) = New SourceGrid.Cells.Cell("")
    '            Grid1(nextRowIndex, 7).View = rowCOLSPAN
    '            Dim rowDetail() As DataRow
    '            rowDetail = dtPhieuNhapKho.Select("TenSanPham='" & aTenSanPhamKhongTrung(i) & "'")
    '            nextRowIndex = nextRowIndex + 1
    '            Dim TongCongTheoCtu As Single = 0
    '            Dim TongCongThuc As Single = 0
    '            For j As Integer = 0 To rowDetail.Length - 1
    '                Grid1.Rows.Insert(nextRowIndex)
    '                Grid1(nextRowIndex, 0) = New SourceGrid.Cells.Cell("")
    '                Dim SoLuongTheoCTu As Single
    '                SoLuongTheoCTu = Single.Parse(rowDetail(j)("SoLuongTheoCTu"))
    '                If SoLuongTheoCTu > 1 Then
    '                    SoLuongTheoCTu = Single.Parse(SoLuongTheoCTu)
    '                End If
    '                Grid1(nextRowIndex, 1) = New SourceGrid.Cells.Cell(SoLuongTheoCTu.ToString & "x" & rowDetail(j)("DVTSanPham"))
    '                Grid1(nextRowIndex, 1).View.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter
    '                Grid1(nextRowIndex, 2) = New SourceGrid.Cells.Cell()
    '                'tính toán ra số lượng
    '                Dim sSoLuongTheoCTu As Single
    '                sSoLuongTheoCTu = TinhSoLuongNhap(rowDetail(j)("IDSanPham"), rowDetail(j)("SoLuongTheoCTu"))
    '                TongCongTheoCtu = TongCongTheoCtu + sSoLuongTheoCTu
    '                Grid1(nextRowIndex, 3) = New SourceGrid.Cells.Cell(Format(sSoLuongTheoCTu, "0.00"))
    '                Dim sSoLuongThuc As Single
    '                sSoLuongThuc = TinhSoLuongNhap(rowDetail(j)("IDSanPham"), rowDetail(j)("SoLuongThuc"))
    '                TongCongThuc = TongCongThuc + sSoLuongThuc
    '                Grid1(nextRowIndex, 4) = New SourceGrid.Cells.Cell("")
    '                If sSoLuongThuc > 0 Then
    '                    Grid1(nextRowIndex, 4).Value = Format(sSoLuongThuc, "0.00")
    '                Else
    '                    Grid1(nextRowIndex, 4).Value = ""
    '                End If
    '                Grid1(nextRowIndex, 5) = New SourceGrid.Cells.Cell("")
    '                Grid1(nextRowIndex, 6) = New SourceGrid.Cells.Cell("")
    '                Grid1(nextRowIndex, 7) = New SourceGrid.Cells.Cell(rowDetail(j)("IDSanPham"))
    '                nextRowIndex = nextRowIndex + 1
    '            Next
    '            Grid1(currentHeaderIndex, 3).Value = Format(TongCongTheoCtu, "0.00")
    '            If TongCongThuc > 0 Then
    '                Grid1(currentHeaderIndex, 4).Value = Format(TongCongThuc, "0.00")
    '            End If
    '        Next
    '    Catch ex As Exception

    '    End Try
    'End Sub
    Private Function TinhSoLuongNhap(ByVal IDSanPham As String, ByVal SoLuongTheoCTu As Single) As Single
        Try
            Dim dataAccess As New DataAccess
            Dim dbConn As New OleDb.OleDbConnection
            Dim SoLuong As Single
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            Dim dbCommand As OleDbCommand
            Dim query As String = "Select DVTSanPham.SoLuong From SanPham" & _
                                    " Left Outer Join DVTSanPham On DVTSanPham.DVTSanPhamId=SanPham.DVTSanPhamId" & _
                                    " Where SanPham.IDSanPham =" & IDSanPham

            dbCommand = New OleDbCommand(query, dbConn)
            If IsDBNull(dbCommand.ExecuteScalar) Then
                SoLuong = 0
            Else
                SoLuong = dbCommand.ExecuteScalar()
            End If
            dbConn.Close()
            Dim sResult As Single = SoLuong * SoLuongTheoCTu
            sResult = Format(sResult, "0.00")
            Return sResult
        Catch ex As Exception
            ShowError()
        End Try
    End Function
#End Region
    Protected Function Create_ChiTietPhieuNhapKho() As DataTable
        Dim dtTable As DataTable = New DataTable()
        Try
            Dim STT As DataColumn = New DataColumn("STT")
            STT.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(STT)

            Dim PhieuNhapKhoId As DataColumn = New DataColumn("PhieuNhapKhoId")
            PhieuNhapKhoId.DataType = System.Type.GetType("System.Int32")
            dtTable.Columns.Add(PhieuNhapKhoId)

            Dim IDSanPham As DataColumn = New DataColumn("IDSanPham")
            IDSanPham.DataType = System.Type.GetType("System.Int32")
            dtTable.Columns.Add(IDSanPham)

            Dim DonGia As DataColumn = New DataColumn("DonGia")
            DonGia.DataType = System.Type.GetType("System.String")
            DonGia.DefaultValue = ""
            dtTable.Columns.Add(DonGia)

            Dim MaSanPham As DataColumn = New DataColumn("MaSanPham")
            MaSanPham.DataType = System.Type.GetType("System.String")
            MaSanPham.DefaultValue = ""
            dtTable.Columns.Add(MaSanPham)

            Dim TenSanPham As DataColumn = New DataColumn("TenSanPham")
            TenSanPham.DataType = System.Type.GetType("System.String")
            TenSanPham.DefaultValue = ""
            dtTable.Columns.Add(TenSanPham)

            Dim DVT As DataColumn = New DataColumn("DVTSanPham")
            DVT.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(DVT)

            Dim QuyCach As DataColumn = New DataColumn("DVT")
            QuyCach.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(QuyCach)

            Dim SoLuongTheoCTu As DataColumn = New DataColumn("SoLuongTheoCTu")
            SoLuongTheoCTu.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(SoLuongTheoCTu)

            Dim SoLuongThuc As DataColumn = New DataColumn("SoLuongThuc")
            SoLuongThuc.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(SoLuongThuc)

            Dim NgayNhapPhieu As DataColumn = New DataColumn("NgayNhapPhieu")
            NgayNhapPhieu.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(NgayNhapPhieu)

            Dim NgayNhap As DataColumn = New DataColumn("NgayNhap")
            NgayNhap.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(NgayNhap)

            Dim ThangNhap As DataColumn = New DataColumn("ThangNhap")
            ThangNhap.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(ThangNhap)

            Dim NamNhap As DataColumn = New DataColumn("NamNhap")
            NamNhap.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(NamNhap)

            Dim NgayNhapTheoSo As DataColumn = New DataColumn("NgayNhapTheoSo")
            NgayNhapTheoSo.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(NgayNhapTheoSo)

            'Dim DonGia As DataColumn = New DataColumn("DonGia")
            'DonGia.DataType = System.Type.GetType("System.String")
            'dtTable.Columns.Add(DonGia)

            Dim ThanhTien As DataColumn = New DataColumn("ThanhTien")
            ThanhTien.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(ThanhTien)

            Return dtTable
        Catch ex As Exception
            Throw
        End Try
    End Function
#End Region

End Class